package org.bluedream.core.module.sys.config.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import lombok.extern.slf4j.Slf4j;
import org.bluedream.comm.base.BaseServiceImpl;
import org.bluedream.comm.utils.EncryForRSAUtil;
import org.bluedream.core.module.sys.config.dao.CertDao;
import org.bluedream.core.module.sys.config.entity.Cert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Service
@DS("master")
@Slf4j
public class CertService extends BaseServiceImpl<CertDao, Cert> implements IService<Cert> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public HashMap<String , Object> secretInfo(){
        try {
            HashMap<String , Object> map = EncryForRSAUtil.genKeyPair();
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public HashMap<String , String> encryptSecret(HashMap<String , Object> arg){
        try {
            // 待加密数据
            Map<String , String> map = new LinkedHashMap<>();
            map.put("publicKey" , EncryForRSAUtil.getPublicKey(arg));
            map.put("privateKey" , EncryForRSAUtil.getPrivateKey(arg));
            String jsonString = JSONObject.toJSONString(map);
            // 生成签名
            String sign = EncryForRSAUtil.sign(jsonString , EncryForRSAUtil.getPrivateKey(arg));

            // 将密钥加密
            HashMap<String , Object> newSecret = EncryForRSAUtil.genKeyPair();
            String encodeString = EncryForRSAUtil.encryptByRSAKey(jsonString , EncryForRSAUtil.getPublicKey(newSecret) , EncryForRSAUtil.PUBLIC_KEY);
            HashMap<String , String> result = new HashMap<>();
            result.put("certificate" , encodeString);
            result.put("sign" , sign);
            result.put("salt" , EncryForRSAUtil.getPrivateKey(newSecret));
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private boolean certIsEmpty(){
        try {
            QueryWrapper<Cert> qw = new QueryWrapper<>(new Cert());
            return super.list(qw).size() == 0?true:false;
        }catch (Exception e){
            return true;
        }
    }

    public boolean save(){
        try {
            HashMap<String , String> source = encryptSecret(secretInfo());
            Cert cert;
            if (certIsEmpty()){
                cert = new Cert();
                cert.setIsNewRecord(true);
            }else {
                cert = baseMapper.getOnlyOne();
                cert.setIsNewRecord(false);
            }
            cert.setCertificate(source.get("certificate"));
            cert.setSign(source.get("sign"));
            cert.setSalt(source.get("salt"));
            System.out.println(cert);
            return super.save(cert);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

/*    public static void main(String[] args) {
        CertService service = new CertService();
        HashMap<String , String> source = service.encryptSecret(service.secretInfo());
    }*/
}
